package mx.com.mindbits.bandpay.dao;

import java.util.List;

import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.dao.DaoSession;
import mx.com.mindbits.dao.contactsDao.Properties;
import mx.com.mindbits.dao.users;
import mx.com.mindbits.dao.usersDao;
import android.content.Context;
import android.util.Log;

public class UsersDAO extends ConnectionFactory{
	
	private DaoSession daoSession;
	private usersDao _userDao;
	
	public UsersDAO(Context ctx) {
		super(ctx);
		daoSession =  daoMaster.newSession();
		_userDao = daoSession.getUsersDao();
	}
	//insert new user
	public boolean insertUser(users user){
		boolean bandera=false;
		try{
			Log.i(ConstantsValues.TAG_DAO,"userDAO" + user.getUser_email());
			_userDao.insert(user);
			bandera = true;
		}catch(Exception e){

			Log.e(ConstantsValues.TAG_DAO, "userDAO" + e);
			//bandera = false;
		}
		return bandera;
	}
	
	//Update User
	public boolean updateUser(users user){
		boolean flag=false;
		try{
			_userDao.update(user);
			flag =  true;
		}catch(Exception e){
			//return false;
		}
		return flag;
	}
	
	//Return List all users
	public List<users> getAllUsers(){
		List<users> listUsers = null;
		try{
			listUsers= _userDao.loadAll();
		}catch(Exception e){
			e.printStackTrace();
		}
		return listUsers;
	}
	
	//Return one user at time
	public users getUserById(Long id_user){
		users _user;//=null;
		try{
			 _user = _userDao.load(id_user);
		}catch(Exception e){
			 _user = null;
			 e.printStackTrace();
		}
		return _user;
	}
	
	public users getLastUser(){
		users _user = null;
		_user = _userDao.queryBuilder().orderDesc(mx.com.mindbits.dao.usersDao.Properties.Id_user).limit(1).unique();
	 return _user;
	}
	
}
